from django.core.management.base import BaseCommand

from core.models import UnicodeRange
from core.utils.const import UNICODE_RANGES


class Command(BaseCommand):
    help = 'Fills database with predefined unicode ranges'

    def handle(self, *args, **options):
        skipped = added = 0

        for urange in UNICODE_RANGES:
            value, name = urange

            start, end = value.split('-')

            try:
                instance = UnicodeRange.objects.get(start=start, end=end)

                skipped += 1

                continue
            except UnicodeRange.DoesNotExist:
                instance = UnicodeRange.objects.create(
                    start=start,
                    end=end, name=name
                )

            print(f'Created UnicodeRange: {instance}')

            added += 1

        print(f'Added: {added} | Skipped: {skipped}')
